package com.campus.campusdelivery.repository;

import com.campus.campusdelivery.entity.DeliveryPerson;
import com.campus.campusdelivery.constant.DeliveryStatus;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface DeliveryPersonRepository extends JpaRepository<DeliveryPerson, Long> {
    
    Optional<DeliveryPerson> findByUserId(Long userId);
    
    List<DeliveryPerson> findByStatus(DeliveryStatus status);
    
    @Query("SELECT dp FROM DeliveryPerson dp WHERE dp.status = :status ORDER BY dp.rating DESC")
    List<DeliveryPerson> findAvailableDeliveryPersons(@Param("status") DeliveryStatus status);
    
    @Query("SELECT COUNT(dp) FROM DeliveryPerson dp WHERE dp.status = :status")
    long countByStatus(@Param("status") DeliveryStatus status);
    
    @Query("SELECT dp FROM DeliveryPerson dp WHERE dp.currentLatitude BETWEEN :minLat AND :maxLat AND dp.currentLongitude BETWEEN :minLng AND :maxLng")
    List<DeliveryPerson> findByLocationRange(@Param("minLat") Double minLat, 
                                           @Param("maxLat") Double maxLat,
                                           @Param("minLng") Double minLng,
                                           @Param("maxLng") Double maxLng);
}
